﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using HFK.DxFluentUITest.Dummy.DataObjects;
using HFK.DxFluentUI.Utility;
using MbUnit.Framework;

namespace HFK.DxFluentUITest.Utility
{
  [TestFixture]
  public class ChildPropertyDescriptorTestFixture
  {
    [Test]
    public void TestZeroHopDescriptor()
    {
      const int intTestGetValue = 1;
      const int intTestSetValue = 2;
      SimpleClass testObject = new SimpleClass() { IntValue = intTestGetValue };
      ChildPropertyDescriptor childPropertyDescriptor = new ChildPropertyDescriptor(typeof(SimpleClass), "IntValue");

      Assert.IsInstanceOfType(typeof(PropertyDescriptor), childPropertyDescriptor);

      PropertyDescriptor propertyDescriptor = childPropertyDescriptor as PropertyDescriptor;

      Assert.AreEqual("IntValue", propertyDescriptor.DisplayName);
      Assert.AreEqual("IntValue", propertyDescriptor.Name);
      Assert.AreEqual(typeof(int), propertyDescriptor.PropertyType);

      Assert.AreEqual(intTestGetValue, propertyDescriptor.GetValue(testObject));
      propertyDescriptor.SetValue(testObject, intTestSetValue);
      Assert.AreEqual(intTestSetValue, testObject.IntValue);
    }

    [Test]
    public void TestSingleHopDescriptor()
    {
      const string stringTestGetValue = "1";
      const string stringTestSetValue = "2";
      SimpleClass testObject = new SimpleClass() { ShcData = new SingleHopClass() { StringValue = stringTestGetValue } };
      ChildPropertyDescriptor childPropertyDescriptor = new ChildPropertyDescriptor(typeof(SimpleClass), "ShcData.StringValue");

      Assert.IsInstanceOfType(typeof(PropertyDescriptor), childPropertyDescriptor);

      PropertyDescriptor propertyDescriptor = childPropertyDescriptor as PropertyDescriptor;

      Assert.AreEqual("ShcData.StringValue", propertyDescriptor.DisplayName);
      Assert.AreEqual("ShcData.StringValue", propertyDescriptor.Name);
      Assert.AreEqual(typeof(string), propertyDescriptor.PropertyType);

      Assert.AreEqual(stringTestGetValue, propertyDescriptor.GetValue(testObject));
      propertyDescriptor.SetValue(testObject, stringTestSetValue);
      Assert.AreEqual(stringTestSetValue, testObject.ShcData.StringValue);
    }

    [Test]
    public void TestTripleHopDescriptor()
    {
      const string stringTestGetValue = "1";
      const string stringTestSetValue = "2";
      SimpleClass testObject = new SimpleClass()
      {
        ShcData = new SingleHopClass()
        {
          DhcProperty = new DualHopClass()
          {
            ThcProperty = new TripleHopClass()
            {
              StringProperty = stringTestGetValue
            }
          }
        }
      };
      ChildPropertyDescriptor childPropertyDescriptor = new ChildPropertyDescriptor(typeof(SimpleClass), "ShcData.DhcProperty.ThcProperty.StringProperty");

      Assert.IsInstanceOfType(typeof(PropertyDescriptor), childPropertyDescriptor);

      PropertyDescriptor propertyDescriptor = childPropertyDescriptor as PropertyDescriptor;

      Assert.AreEqual("ShcData.DhcProperty.ThcProperty.StringProperty", propertyDescriptor.DisplayName);
      Assert.AreEqual("ShcData.DhcProperty.ThcProperty.StringProperty", propertyDescriptor.Name);
      Assert.AreEqual(typeof(string), propertyDescriptor.PropertyType);

      Assert.AreEqual(stringTestGetValue, propertyDescriptor.GetValue(testObject));
      propertyDescriptor.SetValue(testObject, stringTestSetValue);
      Assert.AreEqual(stringTestSetValue, testObject.ShcData.DhcProperty.ThcProperty.StringProperty);
    }
  }
}
